<?php
namespace app\index\controller;

use think\View;
class Index{
    
    /**
     * @var \think\View 视图类实例
     */
    protected $view;
    protected $temp_path = '/';
    
    /* 空操作，用于输出404页面 */
    public function _empty() {
        return 404;
    }
    
    public function __construct(){
        $this->view = View::instance(config('template'), config('view_replace_str'));
        
        $base    = request()->root();
        $root    = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
        if ('' != $root) {
            $root = '/' . ltrim($root, '/');
        }
        
        config('web_path',$root.'/');
        
        /* 读取数据库中的配置 */
        $config =   cache('DB_CONFIG_DATA');
        if(!$config){
            $config =  config_lists();
            cache('DB_CONFIG_DATA',$config);
        }
        config($config); //添加配置
        
        $this->assign('vars',$config);
    }
    
    
    /**
    * 首页
    * @date: 2018年8月20日 下午1:40:41
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function index(){
        
        $this->assign('title',config('seo_title'));
        $this->assign('keywords',config('seo_keywords'));
        $this->assign('description',config('seo_description'));
        
        $html = $this->fetch($this->temp_path.'index');
        return $html;
    }
    
    /**
    * 列表页
    * @date: 2018年8月20日 下午1:40:59
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function lists(){
        $tid = intval(input('id',0));
        $tinfo = db('column')->find($tid);
        
        if($tid <= 0 || empty($tinfo)){
            header("location:".config('web_url')); exit;
        }
        if($tinfo['type'] == 3){
            header("location:".$tinfo['dir']); exit;
        }
        $tmep_file = $tinfo['type'] == 1?$tinfo['temp_index']:$tinfo['temp_list'];
        $minfo = db('models')->find($tinfo['mid']);
        
        $col = db('column')->field('id,pid,type,title,dir')->where(['mid'=>$tinfo['mid']])->order('id asc')->select();
        import('util.Tree');
        $cids = \Tree::getChildsIds($col,$tid,true);
        $parents = \Tree::getParents($col,$tid,true);
        $this->assign('parents',get_pos($tid,$parents));
        
        $topid = 0;
        foreach ($parents as $val){
            if($val['pid']==0){
                $topid = $val['id'];
            }
        }
        $tinfo['topid'] = $topid;
        
        if($tinfo['type'] == 2){
            $page_size = $tinfo['page_size']>0?$tinfo['page_size']:10;
            $map['tid'] = ['in',$cids];
            $list = db($minfo['name'])->where($map)->paginate($page_size,false,['query'=>input('get.')]);
            $page = $list->render();
            $data = $list->toArray();
            $list = $data['data'];
            foreach ($list as $key=>$val){
                $list[$key]['url'] = rurl(['type'=>10,'dir'=>$tinfo['dir']],'view',['id'=>$val['id']]);
            }
            
            $this->assign('list', $list);
            $this->assign('page', $page);
        }
        
        $this->assign('title',$tinfo['title'].' - '.config('seo_title'));
        $this->assign('keywords',!empty($tinfo['keywords'])?$tinfo['keywords']:config('seo_keywords'));
        $this->assign('description',!empty($tinfo['description'])?$tinfo['description']:config('seo_description'));
        $this->assign('tinfo',$tinfo);
        $tmep_file = substr($tmep_file,0,-5);
        $html = $this->fetch($this->temp_path.$tmep_file);
        
        return $html;
    }
    
    /**
    * 内容页
    * @date: 2018年8月20日 下午1:41:39
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function view(){
        $id = intval(input('id',0));
        $ainfo = db('arctiny')->where(['aid'=>$id])->find();
        if(empty($ainfo)){
            header("location:".config('web_url')); exit;
        }
        
        $tinfo = db('column')->find($ainfo['tid']);
        if(empty($tinfo)){
            header("location:".config('web_url')); exit;
        }
        
        $minfo = db('models')->find($tinfo['mid']);
        
        $info = db($minfo['name'])->where(['display'=>1,'id'=>$id])->find();
        if(empty($info)){
            header("location:".config('web_url')); exit;
        }
        
        $this->assign('title',$info['title'].' - '.$tinfo['title'].' - '.config('seo_title'));
        $this->assign('keywords',!empty($info['keywords'])?$info['keywords']:config('seo_keywords'));
        $this->assign('description',!empty($info['description'])?$info['description']:config('seo_description'));
        $this->assign('parents',get_pos($tinfo['id']));
        $this->assign('tinfo',$tinfo);
        $this->assign('info',$info);
        
        $tmep_file = trim($tinfo['temp_view']) != ''?$tinfo['temp_view']:'default_view.html';
        $tmep_file = substr($tmep_file,0,-5);
        $html = $this->fetch($this->temp_path.$tmep_file);
        
        return $html;
    }
    
    
    /**
     * 加载模板输出
     * @access protected
     * @param  string $template 模板文件名
     * @param  array  $vars     模板输出变量
     * @param  array  $replace  模板替换
     * @param  array  $config   模板参数
     * @return mixed
     */
    protected function fetch($template = '', $vars = [], $replace = [], $config = [])
    {
        return $this->view->fetch($template, $vars, $replace, $config);
    }
    
    /**
     * 模板变量赋值
     * @access protected
     * @param  mixed $name  要显示的模板变量
     * @param  mixed $value 变量的值
     * @return $this
     */
    protected function assign($name, $value = '')
    {
        $this->view->assign($name, $value);
    
        return $this;
    }
}